package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.util.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;

/**
 * User: fqy
 * Date: 2021/10/15
 * Time: 19:09
 * Describe:
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

//    @PostMapping("list")
//    public PageResult getCustomerPage(@RequestParam QueryPageBean queryPageBean){
//        return customerService.getCustomerPage(queryPageBean);
//    }

    /**
     * 用户管理：分页查询用户列表 http://localhost:8080 /customer/list
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("list")
    public PageResult getCustomerPage(Integer page, Integer rows, String customerName){
        return customerService.getCustomerPage(page,rows,customerName);
    }
    // 用户管理：保存用户
    @PostMapping("save")
    public ServiceVO saveCustomer(Customer customer,Integer customerId){
        if(customerId==null){
            // 用户管理：添加
            int i = customerService.addCustomer(customer);
            if (i<=0){
                // 添加失败
                return new ServiceVO(200, "保存失败");
            }
        }else{
            // 用户管理：修改
            int j = customerService.updateCustomer(customer,customerId);
            if (j<=0){
                // 添加失败
                return new ServiceVO(200, "保存失败");
            }
        }
        return new ServiceVO(100, "保存成功");
    }
    // 用户管理：删除用户
    @PostMapping("delete")
    public ServiceVO deleteCustomerBatchByIds(String ids){
        String[] arr = ids.split("[\\,]");
        int i = customerService.deleteCustomerBatchByIds(arr);
        if(i>0){
            // 删除成功
            return new ServiceVO(100, "删除成功");
        }else{
            // 删除失败
            return new ServiceVO(200, "删除失败");
        }
    }
}
